package com.gitee.wsl.mathematics.geometry.shape

import androidx.compose.runtime.Stable
import androidx.compose.ui.geometry.Offset
import kotlin.math.sqrt

typealias Point = Offset

@Stable
internal fun Point(x: Int , y: Int ) = Offset(x.toFloat(), y.toFloat())

@Stable
internal fun Point(x: Float = 0f, y: Float = 0f) = Offset(x, y)

@Stable
internal fun Point(x: Double, y: Double) = Offset(x.toFloat(), y.toFloat())

/**
 * Dot product of 2 vectors.
 */
internal operator fun Point.times(vector: Point): Float = dot(vector)

internal operator fun Float.times(vector: Point): Point = vector.times(this)

/**
 * Calculates the dot product of two vectors
 */
internal fun Offset.dot(other: Offset): Float = this.x * other.x + this.y * other.y


/**
 * Calculates Euclidean distance between two vectors
 */
internal fun Point.distance(other: Point): Float = this.copy().sub(other).mag()

internal fun Point.distance(x:Float,y:Float)= distance(Point(x,y))

/**
 * To subtract a vector from [this] vector
 */
internal fun Offset.sub(other: Offset): Offset {
    return Offset(this.x - other.x,    this.y - other.y)
}

/**
 * Calculates the magnitude (length) of the vector and returns the result asa float
 */
internal fun Offset.mag(): Float = sqrt(this.magSq())

/**
 * Calculates the squared magnitude of [this] vector and returns the result as a float
 */
internal fun Offset.magSq(): Float = this.x * this.x + this.y * this.y